package com.inossem.warehousestorage_inossem.activity.receive;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.RegexUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiveItemBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ReceiveUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

public class ReceiveMaterielDetailActivity extends BaseActivity {


    private TextView title, serialNumber, materiel, materielDescribe, manufactor, totalNumber, deliveryNumber, inStorageNumber, receiver, receiveDate, disposeWayDescription, disposeWayValue;

    private EditText receiveNumber, qualifiedNumber;

    private String serialNumberStr, unit;

    private ReceiveReceiveItemBean.DataBean.GoodsListBean goodsItem;

    private boolean mReadOnly;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_materiel_detail);
        initView(ReceiveMaterielDetailActivity.this);
        initData(ReceiveMaterielDetailActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        serialNumber = (TextView) activity.findViewById(R.id.serialNumber);
        materiel = (TextView) activity.findViewById(R.id.materiel);
        materielDescribe = (TextView) activity.findViewById(R.id.materielDescribe);
        manufactor = (TextView) activity.findViewById(R.id.manufactor);
        totalNumber = (TextView) activity.findViewById(R.id.totalNumber);
        deliveryNumber = (TextView) activity.findViewById(R.id.deliveryNumber);
        inStorageNumber = (TextView) activity.findViewById(R.id.inStorageNumber);
        receiver = (TextView) activity.findViewById(R.id.receiver);
        receiveNumber = (EditText) activity.findViewById(R.id.receiveNumber);
        qualifiedNumber = (EditText) activity.findViewById(R.id.qualifiedNumber);
        receiveDate = (TextView) activity.findViewById(R.id.receiveDate);
        disposeWayDescription = (TextView) activity.findViewById(R.id.disposeWayDescription);
        disposeWayValue = (TextView) activity.findViewById(R.id.disposeWayValue);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_materiel_detail_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_receive_item_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (mReadOnly) {
                    activity.finish();
                } else {
                    String checkResult = check(activity);
                    if (checkResult == null) {
                        updateInfo(activity);
                        activity.finish();
                    } else {
                        DialogUtils.getConfirmDialog(activity, checkResult + "\n" + "是否强制退出?", new DialogUtils.ConfirmDialogListener() {
                            @Override
                            public void onConfirm(Dialog dialog) {
                                activity.finish();
                            }
                        }).show();
                    }
                }
            }
        });
        getExtra(activity);
        getData(activity);
    }

    private void getExtra(Activity activity) {
        serialNumberStr = activity.getIntent().getStringExtra(ReceiveExtra.SERIAL_NUMBER);
        mReadOnly = activity.getIntent().getBooleanExtra(ReceiveExtra.READ_ONLY, false);
    }

    private void getData(Context context) {
        goodsItem = ReceiveUtils.getGoodsInformationBySerial(context, serialNumberStr);
        unit = goodsItem.getUnit();
        serialNumber.setText(goodsItem.getSerialNumber());
        materiel.setText(goodsItem.getMateriel());
        materielDescribe.setText(goodsItem.getMaterielDescribe());
        manufactor.setText(goodsItem.getManufactor());
        totalNumber.setText(goodsItem.getTotalNum() + unit);
        deliveryNumber.setText(goodsItem.getDeliveryNum() + unit);
        inStorageNumber.setText(goodsItem.getInStorageNum() + unit);
        receiver.setText(goodsItem.getReceiver());
        //到货数量
        if ("".equals(goodsItem.getReceiveNum())) {
            receiveNumber.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            receiveNumber.setHint(mReadOnly ? "" : context.getResources().getString(R.string.activity_receive_receive_item_receive_number));
        } else {
            receiveNumber.setText(context.getResources().getString(R.string.activity_receive_receive_item_receive) + goodsItem.getReceiveNum() + context.getResources().getString(R.string.common_space) + goodsItem.getUnit());
        }

        receiveNumber.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            if (hasFocus) {
                receiveNumber.setText("");
            }
        });
        //到货日期
        if (TextUtils.isEmpty(goodsItem.getReceiveDate())) {
            receiveDate.setText(mReadOnly ? "" : context.getResources().getString(R.string.activity_receive_receive_item_receive_date));
        } else {
            receiveDate.setText(goodsItem.getReceiveDate());
        }
        receiveDate.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                ActivityUtils.getTimePickerDialog(context, context.getResources().getString(R.string.activity_receive_receive_item_receive_date), receiveDate.getText().toString(), new OnDateSetListener() {
                    @Override
                    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                        receiveDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                        receiveDate.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
                    }
                }).show(getSupportFragmentManager(), "timePicker");
            }
        });
        //合格数量
        if (TextUtils.isEmpty(goodsItem.getQualifiedNumber())) {
            qualifiedNumber.setHint(mReadOnly ? "" : context.getResources().getString(R.string.activity_receive_receive_item_qualified_number));
        } else {
            qualifiedNumber.setText(context.getResources().getString(R.string.activity_receive_receive_item_qualified) + goodsItem.getQualifiedNumber() + context.getResources().getString(R.string.common_space) + goodsItem.getUnit());
        }
        qualifiedNumber.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            if (hasFocus) {
                qualifiedNumber.setText("");
            }
        });
        //处理方式
        if (TextUtils.isEmpty(goodsItem.getCodeDescription())) {
            ReceiveReceiveItemBean.DataBean.DisposeWayListBean disPoseWayBean = ReceiveUtils.getDefaultDisposeWay(context);
            disposeWayDescription.setText(mReadOnly ? "" : disPoseWayBean.getCodeDescription());
            disposeWayValue.setText(mReadOnly ? "" : disPoseWayBean.getCodeValue());
        } else {
            disposeWayDescription.setText(goodsItem.getCodeDescription());
            disposeWayValue.setText(goodsItem.getCodeValue());
        }
        disposeWayDescription.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {

                DialogUtils.getDisposeWayDialog(context, ReceiveUtils.getDisposeWayList(context), new DialogUtils.DisposeWayListener() {
                    @Override
                    public void onSelect(Dialog dialog, String description, String value) {
                        dialog.cancel();
                        disposeWayDescription.setText(description);
                        disposeWayDescription.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
                        disposeWayValue.setText(value);
                    }
                }).show();
            }
        });
        if (mReadOnly) {
            receiveNumber.setFocusable(false);
            receiveDate.setClickable(false);
            qualifiedNumber.setFocusable(false);
            disposeWayDescription.setClickable(false);
        }

    }

    private void updateInfo(Context context) {
        goodsItem.setReceiveNum(receiveNumber.getText().toString().replace(context.getResources().getString(R.string.activity_receive_receive_item_receive), "").replace(goodsItem.getUnit(), "").trim());
        goodsItem.setReceiveDate(receiveDate.getText().toString());
        goodsItem.setQualifiedNumber(qualifiedNumber.getText().toString().replace(context.getResources().getString(R.string.activity_receive_receive_item_qualified), "").replace(goodsItem.getUnit(), "").trim());
        goodsItem.setCodeDescription(disposeWayDescription.getText().toString());
        goodsItem.setCodeValue(disposeWayValue.getText().toString());
        ReceiveUtils.updateGoodsItem(context, goodsItem);
    }

    private String check(Context context) {
        String receiveNumberNumberStr = receiveNumber.getText().toString().replace(context.getResources().getString(R.string.activity_receive_receive_item_receive), "").replace(goodsItem.getUnit(), "").trim();
        String deliveryNumberNumberStr = deliveryNumber.getText().toString().replace(goodsItem.getUnit(), "").trim();
        String qualifiedNumberNumberStr = qualifiedNumber.getText().toString().replace(context.getResources().getString(R.string.activity_receive_receive_item_qualified), "").replace(goodsItem.getUnit(), "").trim();
        //检验收货数量
        if ("".equals(receiveNumberNumberStr)) {
            return context.getResources().getString(R.string.activity_receive_receive_item_receive_number);
        } else if (!RegexUtils.checkDecimal(receiveNumberNumberStr)) {
            return context.getResources().getString(R.string.activity_receive_receive_item_not_regex_receive_number);
        } else if (Float.parseFloat(receiveNumberNumberStr) > Float.parseFloat(deliveryNumberNumberStr)) {
            return context.getResources().getString(R.string.activity_receive_receive_item_receive_number_less_delivery_number);
        }
        //检验收货日期
        if (context.getResources().getString(R.string.activity_receive_receive_item_receive_date).equals(receiveDate.getText().toString())) {
            return context.getResources().getString(R.string.activity_receive_receive_item_receive_date);
        }
        //检验合格数量
        if ("".equals(qualifiedNumberNumberStr)) {
            return context.getResources().getString(R.string.activity_receive_receive_item_qualified_number);
        } else if (!RegexUtils.checkDecimal(qualifiedNumberNumberStr)) {
            return context.getResources().getString(R.string.activity_receive_receive_item_not_regex_qualified_number);
        } else if (Float.parseFloat(qualifiedNumberNumberStr) > Float.parseFloat(receiveNumberNumberStr)) {
            return context.getResources().getString(R.string.activity_receive_receive_item_qualified_number_less_receive_number);
        }
        //检验不合格处理方式
        if (context.getResources().getString(R.string.activity_receive_receive_item_treatment_mode).equals(disposeWayDescription.getText().toString())) {
            return context.getResources().getString(R.string.activity_receive_receive_item_treatment_mode);
        }
        return null;
    }

    @Override
    public void onBackPressed() {
        if (mReadOnly) {
            super.onBackPressed();
        } else {
            String checkResult = check(ReceiveMaterielDetailActivity.this);
            if (checkResult == null) {
                updateInfo(ReceiveMaterielDetailActivity.this);
                super.onBackPressed();
            } else {
                DialogUtils.getConfirmDialog(ReceiveMaterielDetailActivity.this, checkResult + "\n" + "是否强制退出?", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        ReceiveMaterielDetailActivity.super.onBackPressed();
                    }
                }).show();
            }
        }
    }
}
